package cn.lili.modules.plus.serviceImpl;

import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.member.service.UserService;
import cn.lili.modules.member.service.VipApplyService;
import cn.lili.modules.member.service.VipCodeService;
import cn.lili.modules.payment.entity.enums.ApplymentStatusEnum;
import cn.lili.modules.plus.service.UrbanVipCodeClientService;
import cn.lili.modules.system.entity.dos.LiVipActiveCode;
import cn.lili.modules.system.entity.dos.LiVipApply;
import cn.lili.modules.system.entity.dto.ApplyCodeDto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencentcloudapi.tcaplusdb.v20190823.models.ApplyStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class UrbanVipCodeClientServiceImpl implements UrbanVipCodeClientService {
    @Autowired
    private VipApplyService applyService;
    @Autowired
    private UserService userService;

    @Override
    public Page<LiVipApply> getList(ApplyCodeDto dto) {
        LambdaQueryWrapper<LiVipApply>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(LiVipApply::getApplySupplierId,dto.getSupplierId())
                .orderByDesc(LiVipApply::getCreateTime)
                .eq(Objects.nonNull(dto.getApplyStatus()),LiVipApply::getApplyStatus,dto.getApplyStatus());
        return applyService.page(new Page<>(dto.getPageNumber(), dto.getPageSize()),wrapper);
    }

    @Override
    public void codeApply(LiVipApply liVipApply) {
        //检测必须合法的参数
        if (liVipApply.getApplyNum()==null||
                liVipApply.getTimeType()==null){
            throw new ServiceException(ResultCode.PARAMS_ERROR);
        }
        User user = userService.getById(liVipApply.getApplySupplierId());
        //User user = userService.getByExtId(liVipApply.getApplySupplierId());
        if (Objects.equals(user,null))
        {
            throw new ServiceException(ResultCode.USER_NOT_EXIST);

        }
        LiVipApply liVipApply1 = LiVipApply.builder()
                .applyStatus(1)
                .applyNum(liVipApply.getApplyNum())
                .applySupplierId(liVipApply.getApplySupplierId())
                .applySupplierName(liVipApply.getApplySupplierName())
                .applyTime(Date.from(Instant.now()))
                .message(liVipApply.getMessage())
                .timeType(liVipApply.getTimeType())
                .applyStatus(1)
                .build();

        applyService.save(liVipApply1);
    }
}
